const express = require('express');
const router = express.Router();
const { userAuth } = require('@middlewares/auth');
const categoryController = require('@controllers/miniapp/category.controller');
const productController = require('@controllers/miniapp/product.controller');
const userController = require('@controllers/miniapp/user.controller');
const cartController = require('@controllers/miniapp/cart.controller');
const bannerController = require('@controllers/miniapp/banner.controller');
const addressController = require('@controllers/miniapp/address.controller');
// 使用新的订单控制器
const orderController = require('../../controllers/miniapp/order.controller');

// 用户相关接口
router.post('/users/wxlogin', userController.wxLogin); // 微信登录（旧路径）
router.post('/users/wx-login', userController.wxLogin); // 微信登录（新路径，兼容前端）
router.post('/users/wx-phone-login', userController.wxPhoneLogin); // 微信登录获取手机号
router.post('/users/send-code', userController.sendVerifyCode); // 发送手机验证码
router.post('/users/phone-login', userController.phoneLogin); // 手机号验证码登录
router.get('/users/info', userAuth, userController.getUserInfo); // 获取用户信息

// Banner接口
router.get('/banners', bannerController.getBanners);

// 分类接口
router.get('/categories', categoryController.getCategories);
router.get('/categories/:id', categoryController.getCategoryDetail);
router.get('/categories/:id/products', categoryController.getCategoryProducts);

// 商品接口
router.get('/products', productController.getProducts);
router.get('/products/recommend', productController.getRecommendProducts);
router.get('/products/:id', productController.getProductDetail);

// 购物车接口 (需要登录)
// 重要：路由顺序很重要，特定路径要放在通配路径之前
router.get('/cart/count', userAuth, cartController.getCartCount);
router.get('/cart', userAuth, cartController.getCart);
router.post('/cart', userAuth, cartController.addToCart);
router.put('/cart/selected/all', userAuth, cartController.updateAllCartItemsSelected);
router.put('/cart/:id/selected', userAuth, cartController.updateCartItemSelected);
router.put('/cart/:id/quantity', userAuth, cartController.updateCartItem);
router.delete('/cart/:id', userAuth, cartController.removeCartItem);

// 地址管理接口 (需要登录)
router.get('/addresses/default', userAuth, addressController.getDefaultAddress);
router.get('/addresses', userAuth, addressController.getAddresses);
router.post('/addresses', userAuth, addressController.createAddress);
router.put('/addresses/:id', userAuth, addressController.updateAddress);
router.delete('/addresses/:id', userAuth, addressController.deleteAddress);
router.post('/addresses/:id/default', userAuth, addressController.setDefaultAddress);

// 添加订单接口 (需要登录)
router.get('/orders', userAuth, orderController.getOrders);
router.get('/orders/recent', userAuth, orderController.getRecentOrders);
router.get('/orders/:id', userAuth, orderController.getOrderById); // 更新为新方法名
router.put('/orders/:id/cancel', userAuth, orderController.cancelOrder); // 更新为PUT方法，符合REST风格
router.post('/orders', userAuth, orderController.createOrder); // 添加创建订单接口
router.post('/orders/:id/pay', userAuth, orderController.payOrder); // 添加支付路由

// 以下API尚未实现
// router.post('/orders/:id/receive', userAuth, orderController.confirmReceipt);
// router.get('/products/hot', productController.getHotProducts);
// router.get('/products/new', productController.getNewProducts);
// router.post('/orders/:id/pay', userAuth, orderController.payOrder);

// 微信支付回调通知接口（不需要验证用户登录）
router.post('/payment/notify', orderController.handlePaymentNotify);

module.exports = router; 